﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace OA.UI
{
    public class Controls
    {
        /// <summary>
        /// 将字典转换成select控件值
        /// </summary>
        /// <param name="parentID">字典父ID</param>
        /// <param name="code">字典代号</param>
        /// <param name="value">控件值，可以为空</param>
        /// <param name="allowNull">是否允许输入空值</param>
        /// <returns></returns>
        public static string SelectByDictionary(string code, string value, bool allowNull)
        {
            var sb = new StringBuilder();
            if (allowNull) sb.Append("<option value=\"\">　</option>");
            var dic = new BLL.Dictionary().GetChilds(code);
            foreach (var d in dic)
            {
                sb.Append("<option value=\"" + d.Code + "\"");
                if (d.Code == value) sb.Append(" selected=\"selected\"");
                sb.Append(">" + d.Code + ":" + d.DictionaryName + "</option>");
            }
            return sb.ToString();
        }

        /// <summary>
        /// 将字典转换成Radio控件
        /// </summary>
        /// <param name="controlName">控件名称</param>
        /// <param name="code">字典代号</param>
        /// <param name="value">被选中的值，可以为空</param>
        /// <param name="attribute">添加到HTML控制上的属性</param>
        /// <returns></returns>
        public static string RadioByDictionary(string controlName, string code, string value,string attribute)
        {
            var sb = new StringBuilder();
            var dic = new BLL.Dictionary().GetChilds(code);
            if (attribute == null) attribute = "";
            for (int i = 0; i < dic.Count; i++)
            {
                sb.Append(string.Format("<span class=\"radiobox_midle\"><input name=\"{0}\" id=\"{0}_{1}\" type=\"radio\" value=\"{1}\" {3} {4}/><label for=\"{0}_{1}\" class=\"defaulFont\">{2}</label></span>"
                    , controlName, dic[i].Code, dic[i].DictionaryName, value == null ? (i == 0 ? "checked=\"checked\"" : "") : (value == dic[i].Code ? "checked=\"checked\"" : ""),attribute));
            }
            return sb.ToString();
        }

        /// <summary>
        /// 将字典转换成checkbox控件
        /// </summary>
        /// <param name="controlName">控件名称</param>
        /// <param name="code">字典代号</param>
        /// <param name="value">被选中的值，以逗号分隔多个，可以为空</param>
        /// <param name="attribute">添加到HTML控制上的属性</param>
        public static string CheckBoxByDictionary(string controlName, string code, string value, string attribute)
        {
            var sb = new StringBuilder();
            var dic = new BLL.Dictionary().GetChilds(code);
            string[] values = { };
            if (!string.IsNullOrEmpty(value)) values = value.Split(',');
            for (int i = 0; i < dic.Count; i++)
            {
                sb.Append(string.Format("<span class=\"checkbox_midle\"><input name=\"{0}\" id=\"{0}_{1}\" type=\"checkbox\" value=\"{1}\" {3} {4}/><label for=\"{0}_{1}\" class=\"defaulFont\">{2}</label></span>"
                    , controlName, dic[i].Code, dic[i].DictionaryName, values.Any(r => r == dic[i].Code) ? "checked=\"checked\"" : "",attribute));
            }
            return sb.ToString();
        }
    }
}
